package com.ldj.struts.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.common.Upload;
import com.ldj.hibernate.MyFileInfo;
import com.ldj.hibernate.MyFileInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.MyFileInfoForm;

public class MyFileInfoAction extends DispatchAction {

	public ActionForward myFilelist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MyFileInfoForm mfForm = (MyFileInfoForm)form;
		MyFileInfoDAO mfDao = new MyFileInfoDAO(request, response);
		List myFileList = mfDao.findAllPage(mfForm);
		request.setAttribute("list", myFileList);
		return mapping.findForward("myFile");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MyFileInfoDAO dao = new MyFileInfoDAO(request, response);
		MyFileInfo mfInfo = dao.findById(request.getParameter("myFileInfoId"));
		request.setAttribute("mfInfo", mfInfo);
		return mapping.findForward("myFileDetail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			MyFileInfoDAO dao = new MyFileInfoDAO(request, response);
			MyFileInfo mfInfo = dao.findById(request.getParameter("myFileInfoId"));
			request.setAttribute("mfInfo", mfInfo);	
			return mapping.findForward("toUpdate");
	}
	
	
	public ActionForward toSend(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			MyFileInfoDAO dao = new MyFileInfoDAO(request, response);
			MyFileInfo mfInfo = dao.findById(request.getParameter("myFileInfoId"));
			request.setAttribute("mfInfo", mfInfo);	
			return mapping.findForward("toSend");
	}
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MyFileInfoForm mfForm = (MyFileInfoForm)form;
		MyFileInfoDAO mfInfoDao = new MyFileInfoDAO(request, response);
		MyFileInfo mfInfo = mfInfoDao.findById(request.getParameter("myFileInfoId"));
		mfInfo.setMyFileInfoFileDescription(mfForm.getMyFileInfoFileDescription());
		mfInfo.setMyFileInfoFileTitle(mfForm.getMyFileInfoFileTitle());
		mfInfo.setMyFileInfoFileUrl(mfForm.getMyFileInfoFileUrl());
		mfInfoDao.merge(mfInfo);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "MyFileInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", mfInfo.getMyFileInfoFileTitle()));
		logDao.save(s);
		return new ActionForward("/MyFileInfo.do?method=myFilelist", true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
				MyFileInfoForm mfForm = (MyFileInfoForm)form;
				MyFileInfo mfInfo = new MyFileInfo();
				MyFileInfoDAO mfInfoDao = new MyFileInfoDAO(request, response);
				mfInfo.setMyFileInfoOwnerId(SystemSession.getSupUserId(request));
				mfInfo.setMyFileInfoFileDescription(mfForm.getMyFileInfoFileDescription());
				mfInfo.setMyFileInfoFileTitle(mfForm.getMyFileInfoFileTitle());
				mfInfo.setMyFileInfoFileUrl(mfForm.getMyFileInfoFileUrl());
				mfInfo.setMyFileInfoCreateTime(new Date());
				mfInfo.setMyFileInfoDelOper("0");
				mfInfoDao.save(mfInfo);
				
				SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
				SysLogInfo s = new SysLogInfo();
				s.setPersionFile(SystemSession.getSupUserInfo(request));
				s.setSysLogInfoOperDate(new Date());
				s.setSysLogInfoOperType("0");
				s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
				s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "MyFileInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", mfInfo.getMyFileInfoFileTitle()));
				logDao.save(s);
				return new ActionForward("/MyFileInfo.do?method=myFilelist", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		    MyFileInfoDAO mfInfoDao = new MyFileInfoDAO(request, response);
		    MyFileInfo mfInfo = mfInfoDao.findById(request.getParameter("myFileInfoId"));
		    mfInfo.setMyFileInfoDelOper("1");
			mfInfoDao.merge(mfInfo);
			
			SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
			SysLogInfo s = new SysLogInfo();
			s.setPersionFile(SystemSession.getSupUserInfo(request));
			s.setSysLogInfoOperDate(new Date());
			s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
			s.setSysLogInfoOperType("2");
			s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "MyFileInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", mfInfo.getMyFileInfoFileTitle()));
			logDao.save(s);
		return new ActionForward("/MyFileInfo.do?method=myFilelist", true);
	}
	
	public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HashMap map = Upload.upload(request, "myFile", ".doc,.xls");
			if(map.containsKey("fileName")){
				request.setAttribute("fileName", (String)map.get("fileName"));
			}else{
				request.setAttribute("error", "error");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapping.findForward("upload");
	}
}